package com.qf.fmall.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall.category.entity.Category;
import com.qf.fmall.category.mapper.CategoryMapper;
import com.qf.fmall.category.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall.product.entity.Product;
import com.qf.fmall.product.entity.ProductImg;
import com.qf.fmall.product.mapper.ProductImgMapper;
import com.qf.fmall.product.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author wbw
 * @since 2023-11-28
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> categoryList() {
        //1.查询所有一级分类
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("category_level",1);
        List<Category> leve1Data = categoryMapper.selectList(categoryQueryWrapper);
        //2.遍历所有一级分类，查询下属的二级分类
        for (Category level1Cat : leve1Data) {
            Integer level1Id = level1Cat.getCategoryId();
            List<Category> level2Data = categoryMapper.selectList(new QueryWrapper<Category>().eq("parent_id", level1Id));
            //遍历二级分类，查询对应的三级分类
            for (Category level2Cat : level2Data) {
                Integer level2Id = level2Cat.getCategoryId();
                List<Category> level3Data = categoryMapper.selectList(new QueryWrapper<Category>().eq("parent_id", level2Id));
                //把查到的三级分类的数据放到二级分类对象的categories属性中
                level2Cat.setCategories(level3Data);
            }
            //把查到的二级分类的数据放到一级分类对象的categories属性中
            level1Cat.setCategories(level2Data);
        }
        return leve1Data;
    }
    //更好的获取子集  减少数据库的查询次数
    @Override
    public List<Category> categoryListBetter() {
        //1.获取全部的分类数据
        List<Category> allData = categoryMapper.selectList(null);
        //1.1构建一个map，key放分类id,value放分类对象
        HashMap<Integer, Category> map = new HashMap<>();
        for (Category category : allData) {
            map.put(category.getCategoryId(), category);
        }
        //2.处理数据变成我们需要的结构 List<一级分类对象（二级、三级数据）>
        ArrayList<Category> data = new ArrayList<>();
        for (Category category : allData) {
            //如果是一级分类就放入data
            if (category.getCategoryLevel().equals(1)){
                data.add(category);
            }else {
                //如果拿到了二级、三级分类
                //找到该分类的上级分类对象
                // 把当前的分类添加到上级分类的categories list的属性中
                Integer parentId = category.getParentId();
                Category parent = map.get(parentId);
                List<Category> categories = parent.getCategories();

                categories.add(category);
            }
        }
        return data;
    }
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<Category> categoryRecommends() {
        //1.查询所有的分类信息
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("category_level",1);
        List<Category> categories = categoryMapper.selectList(wrapper);
        //2.遍历所有一级分类，查询该分类下的销量前六的商品信息
        for (Category category : categories) {
            Integer categoryId = category.getCategoryId();
            QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
            productQueryWrapper.eq("root_category_id",categoryId);
            //根据商品的销量进行倒序取前6个
            productQueryWrapper.orderByDesc("sold_num");
            productQueryWrapper.last("limit 6");
            List<Product> products = productMapper.selectList(productQueryWrapper);
            //需要给商品添加图片信息
            addPic(products);

            //把商品信息设置到分类信息中
            category.setProducts(products);
        }
        return categories;
    }

    @Autowired
    private ProductImgMapper productImgMapper;
    private void addPic(List<Product> products) {
        for (Product product : products) {
            QueryWrapper<ProductImg> wrapper = new QueryWrapper<>();
            wrapper.eq("product_id",product.getProductId());
            List<ProductImg> productImgs = productImgMapper.selectList(wrapper);
            product.setImgs(productImgs);
        }
    }

}
